// Copyright (c) Microsoft Corporation. All rights reserved.

namespace SPPivotViewer.HttpHandler.Internal
{
    using System;
    using System.Collections.Generic;
    using SPPivotViewer.HttpHandler.Internal.ImageProviders;


    /// <summary>
    /// Used internally to represent an item within a collection
    /// </summary>
    internal class CollectionItem
    {
        // Constructors, Finalizer and Dispose
        //======================================================================

        public CollectionItem()
        {
        }

        // Public Properties
        //======================================================================

        public string Name { get; set; }
        public string Url { get; set; }
        public string Description { get; set; }
        public ImageProviderBase ImageProvider { get; private set;  }
        public ICollection<Facet> FacetValues { get; set; }


        // Public Methods
        //======================================================================

        public void SetImage(ItemImage image)
        {

            Dictionary<string, string> dicMetadata=new Dictionary<string,string>();
            if (FacetValues != null)
            {
                foreach (Facet fct in FacetValues)
                {
                    if (fct.ShowInInfoPane || fct.ShowInFacetPane)
                    {
                        if (fct.Tag is FacetHyperlink)
                            dicMetadata.Add(fct.Category, ((FacetHyperlink)fct.Tag).Name);
                        else
                            dicMetadata.Add(fct.Category, fct.Tag.ToString());
                    }

                }
            }
            if (null == image)
            {
                this.ImageProvider = new DynamicImage(this.Name, dicMetadata);
            }
            else if (null != image.ImageUrl)
            {
                Uri imgUrl = image.ImageUrl;
                if (imgUrl.LocalPath.EndsWith(".dzi", StringComparison.InvariantCultureIgnoreCase))
                {
                    this.ImageProvider = new DeepZoomImage(imgUrl);
                }
                else if (!imgUrl.LocalPath.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase) && !imgUrl.LocalPath.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase) && !imgUrl.LocalPath.EndsWith(".jpeg", StringComparison.InvariantCultureIgnoreCase))
                {
                    this.ImageProvider = new DocumentImage(imgUrl, Name, dicMetadata);
                }
                else
                {

                    this.ImageProvider = new WebImage(imgUrl, Name, dicMetadata);
                }
            }
        }
    }

}
